package controllers;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.model.SelectItem;
import model.entities.StockUpdateStatus;
import myDaos.StockUpdatedDao;

@ManagedBean
@ViewScoped
public class StockUpdateManagedBean {
	private List<StockUpdateStatus> slist;
	private List<String> clist;
	private SelectItem[] countryList;
	private SelectItem[] regionList;
	private String country;
	private String region;
	private boolean isMethodCalled = false;
	private List<SelectItem> countriesList;
	private List<String> countries;


	@ManagedProperty("#{servicesManagedBean}")
	private ServicesManagedBean servicesManagedBean;

	@EJB
	private StockUpdatedDao sud;
	 

	
	
	
	

	public List<String> getCountries() {
		return countries;
	}

	public void setCountries(List<String> countries) {
		this.countries = countries;
	}

	public List<SelectItem> getCountriesList() {
		return countriesList;
	}

	public void setCountriesList(List<SelectItem> countriesList) {
		this.countriesList = countriesList;
	}

	public ServicesManagedBean getServicesManagedBean() {
		return servicesManagedBean;
	}

	public void setServicesManagedBean(ServicesManagedBean servicesManagedBean) {
		this.servicesManagedBean = servicesManagedBean;
	}

	public boolean isMethodCalled() {
		return isMethodCalled;
	}

	public void setMethodCalled(boolean isMethodCalled) {
		this.isMethodCalled = isMethodCalled;
	}

	public List<StockUpdateStatus> getSlist() {
		return slist;
	}

	public void setSlist(List<StockUpdateStatus> slist) {
		this.slist = slist;
	}

	public List<String> getClist() {
		return clist;
	}

	public void setClist(List<String> clist) {
		this.clist = clist;
	}

	public SelectItem[] getCountryList() {
		return countryList;
	}

	public void setCountryList(SelectItem[] countryList) {
		this.countryList = countryList;
	}

	public SelectItem[] getRegionList() {
		return regionList;
	}

	public void setRegionList(SelectItem[] regionList) {
		this.regionList = regionList;
	}

	public String getCountry() {
		return country;
	}

	public void setCountry(String country) {
		this.country = country;
	}

	public String getRegion() {
		return region;
	}

	public void setRegion(String region) {
		this.region = region;
	}

	public void updateList()
	{
		slist = sud.getStockUpadteList(country, region);
		isMethodCalled=true;
	}

	public void updateCountries()
	{
		//Proposed by Samy
		countriesList = new ArrayList<SelectItem>();
		countries = new ArrayList<String>();
		for(int i = 0; i < servicesManagedBean.getCountrieslist().size(); i++) 
	    {  
			if(region!=null && !region.trim().equals("") && 
					  servicesManagedBean.getCountrieslist().get(i).getMapRegion().equals(region))
			{
				if(!countries.contains(servicesManagedBean.getCountrieslist().get(i).getMapCountry())){
					countries.add(servicesManagedBean.getCountrieslist().get(i).getMapCountry());
					countriesList.add(new SelectItem(servicesManagedBean.getCountrieslist().get(i).getMapCountry(),
							servicesManagedBean.getCountrieslist().get(i).getMapCountry()));
				}
			}
	    }
	}



}
